﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace FTPLib
{
   public  class test
    {
       FTPCore core = new FTPCore();

       


       public void ReadFile(string filePath, string fileName, string file_out)
       {
           FtpWebRequest tmpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + core.FTPSERVERIP + "/" + fileName));

           tmpReq.Credentials = new System.Net.NetworkCredential(core.FTPUSERID, core.FTPPASSWORD);

           //GET THE FTP RESPONSE  
           try
           {

               WebResponse tmpRes = tmpReq.GetResponse();
               //GET THE STREAM TO READ THE RESPONSE FROM  
               Stream tmpStream = tmpRes.GetResponseStream();
               //CREATE A TXT READER (COULD BE BINARY OR ANY OTHER TYPE YOU NEED)  
               TextReader tmpReader = new System.IO.StreamReader(tmpStream);
               //STORE THE FILE CONTENTS INTO A STRING  
               string fileContents = tmpReader.ReadToEnd();
               //DO SOMETHING WITH SAID FILE CONTENTS
               
               
               tmpReader.Close();
               tmpRes.Close();


               string regMatch = "CARD ENTERED";
               if (Regex.IsMatch(fileContents, regMatch))
               {
                   try
                   {

                       FileStream xml_out = new FileStream(filePath + "\\" + file_out, FileMode.Create);
                       XmlTextWriter writer = new XmlTextWriter(xml_out, null);
                       writer.Formatting = Formatting.Indented;
                       writer.WriteStartDocument();
                       writer.WriteStartElement("Root");
                       writer.WriteStartElement("Header");
                       writer.WriteStartElement("H1");
                       writer.WriteString(regMatch.TrimEnd());
                       writer.WriteEndElement();
                       writer.WriteEndElement();
                       writer.WriteEndElement();
                       writer.WriteEndDocument();
                       writer.Close();
                       xml_out.Close();
                   }
                   catch (IOException)
                   {
                       MessageBox.Show("Error Opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   }
               }




           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }
    }
}
